#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, m, dsfa;
ll c[100001];
ll a[100001][6];
namespace sol1 {
	ll r[7];
	void get() {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++)
				r[j] += a[i][j];
			r[6] = max(r[6] + a[i][c[i]], r[c[i]]);
			r[c[i]] = r[6];
		}
		cout << r[6];
	}
}
namespace sol2 {
	ll mn = -100000000000ll;
	struct node {
		ll d[5][5][4];
		ll g[6];
	} tr[800001];
	void start(ll i, ll d, ll k) {
		for (int s = 0; s < m; s++)
			for (int j = 0; j < m; j++)
				for (int r = 0; r < 4; r++)
					tr[i].d[s][j][r] = mn, tr[i].g[s] = mn;
		tr[i].g[m] = mn;
		for (int j = 0; j < 4; j++)
			tr[i].d[k][k][j] = a[d][k + 1];
		for (int j = 0; j < m; j++)
			tr[i].g[j] = a[d][j + 1];
		tr[i].g[m] = a[d][k + 1];
	}
	node merge(node a, node b) {
		node res;
		for (int s = 0; s < m; s++)
			for (int j = 0; j < m; j++)
				for (int r = 0; r < 4; r++)
					res.d[s][j][r] = mn, res.g[s] = mn;
		res.g[m] = mn;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++) {
				ll mx1, mx2;
				mx1 = 0, mx2 = 0;
				for (int r = 0; r < m; r++)
					mx1 = max(mx1, a.d[i][r][0]);
				for (int r = 0; r < m; r++)
					mx2 = max(mx2, b.d[r][j][0]);
				res.d[i][j][0] = mx1 + mx2;
				for (int r = 0; r < m; r++)
					res.d[i][j][0] = max(res.d[i][j][0], a.d[i][r][1] + b.d[r][j][2]);
				mx1 = 0, mx2 = 0;
				for (int r = 0; r < m; r++)
					mx1 = max(mx1, a.d[i][r][2]);
				for (int r = 0; r < m; r++)
					mx2 = max(mx2, b.d[r][j][0]);
				res.d[i][j][2] = mx1 + mx2;
				for (int r = 0; r < m; r++)
					res.d[i][j][2] = max(res.d[i][j][2], a.d[i][r][3] + b.d[r][j][2]);
				mx1 = 0, mx2 = 0;
				for (int r = 0; r < m; r++)
					mx1 = max(mx1, a.d[i][r][0]);
				for (int r = 0; r < m; r++)
					mx2 = max(mx2, b.d[r][j][1]);
				res.d[i][j][1] = mx1 + mx2;
				for (int r = 0; r < m; r++)
					res.d[i][j][1] = max(res.d[i][j][1], a.d[i][r][1] + b.d[r][j][3]);
				mx1 = 0, mx2 = 0;
				for (int r = 0; r < m; r++)
					mx1 = max(mx1, a.d[i][r][2]);
				for (int r = 0; r < m; r++)
					mx2 = max(mx2, b.d[r][j][1]);
				res.d[i][j][3] = mx1 + mx2;
				for (int r = 0; r < m; r++)
					res.d[i][j][3] = max(res.d[i][j][3], a.d[i][r][3] + b.d[r][j][3]);
			}
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++) {
				res.d[i][j][0] = max(res.d[i][j][0], max(a.d[i][j][0] + b.g[m], b.d[i][j][0] + a.g[m]));
				res.d[i][j][1] = max(res.d[i][j][1], max(a.d[i][j][1] + b.g[j], b.d[i][j][1] + a.g[m]));
				res.d[i][j][2] = max(res.d[i][j][2], max(a.d[i][j][2] + b.g[m], b.d[i][j][2] + a.g[i]));
				res.d[i][j][3] = max(res.d[i][j][3], max(a.d[i][j][3] + b.g[j], b.d[i][j][3] + a.g[i]));
			}
		for (int i = 0; i <= m; i++)
			res.g[i] = a.g[i] + b.g[i];
		return res;
	}
	node sol(ll i, ll l, ll r, ll x, ll y) {
		if (x <= l && r <= y)
			return tr[i];
		ll mid = (l + r) >> 1;
		if (x >= mid + 1)
			return sol(i << 1 | 1, mid + 1, r, x, y);
		else if (y <= mid)
			return sol(i << 1, l, mid, x, y);
		else
			return merge(sol(i << 1, l, mid, x, y), sol(i << 1 | 1, mid + 1, r, x, y));
	}
	void build(ll i, ll l, ll r) {
		if (l == r) {
			start(i, l, c[l] - 1);
			return;
		}
		ll mid = (l + r) >> 1;
		build(i << 1, l, mid);
		build(i << 1 | 1, mid + 1, r);
		tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
	}
	void been(ll i, ll l, ll r, ll x, ll y) {
		if (l == r) {
			start(i, x, y);
			return;
		}
		ll mid = (l + r) >> 1;
		if (x >= mid + 1)
			been(i << 1 | 1, mid + 1, r, x, y);
		else
			been(i << 1, l, mid, x, y);
		tr[i] = merge(tr[i << 1], tr[i << 1 | 1]);
	}
	void get() {
		build(1, 1, n);
		node res = sol(1, 1, n, 1, n);
		ll lstans = 0;
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++)
				lstans = max(res.d[i][j][0], lstans);
		cout << lstans << '\n';
		for (int i = 1; i <= dsfa; i++) {
			ll tp;
			cin >> tp;
			ll p, q;
			cin >> p >> q;
			p ^= lstans;
			q ^= lstans;
//			cout<<1;
			if (tp == 1) {
				been(1, 1, n, p, q - 1);
			} else {
				node res = sol(1, 1, n, p, q);
				lstans = 0;
				for (int i = 0; i < m; i++)
					for (int j = 0; j < m; j++)
						lstans = max(res.d[i][j][0], lstans);
				cout << lstans << '\n';
			}
//			if (i % 6000 == 0)
//				cout << i;
		}
	}
}
using namespace sol1;
using namespace sol2;
int main() {
	freopen("paint5.in", "r", stdin);
	freopen("paint.out", "w", stdout);
//	ios::sync_with_stdio(false);
//	cin.tie(0), cout.tie(0);
	cin >> n >> m >> dsfa;
	for (int i = 1; i <= n; i++)
		cin >> c[i];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	if (dsfa == 0)
		sol1::get();
	else
		sol2::get();
	return 0;
}

